package com.tang.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.common.api.UserFeignClient;
import com.tang.common.entity.community.News;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.common.utils.JwtUtil;
import com.tang.common.utils.UpLoad;
import com.tang.community.service.NewsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;


    @RequestMapping("/newsList")
    public Result getNewsList(String keywords, Integer currentPage, Integer pageSize, String filter, HttpServletRequest request) {
        if ("all".equals(filter)) {
            PageInfo<News> newsPageInfo = newsService.listNews(keywords, currentPage, pageSize, null);
            if (newsPageInfo != null) {
                return Result.success(newsPageInfo);
            }
        }
        if ("my".equals(filter)) {
            String token = request.getHeader("TOKEN");
            String adminName = JwtUtil.get(token, "username");
            PageInfo<News> newsPageInfo = newsService.listNews(keywords, currentPage, pageSize, adminName);
            if (newsPageInfo != null) {
                return Result.success(newsPageInfo);
            }
        }
        QueryWrapper<News> query = new QueryWrapper<>();
        query.eq("delete_flag",1);
        query.eq("state",1);
        query.orderByDesc("create_time");
        PageHelper.startPage(currentPage, pageSize);
        List<News> list = newsService.list(query);
        if(list!=null){
            return Result.success(PageInfo.of(list));
        }
        return Result.error(RecruitExceptionEnum.NO_DATA);
    }

    @ApiOperation("更新资讯")
    @RequestMapping("/updateNews/upload")
    public Result updateNews(@RequestParam("newsId") Integer newsId,
                             @RequestParam("newsTitle") String newsTitle,
                             @RequestParam("newsContent") String newsContent,
                             @RequestParam("img") List<MultipartFile> imgs) {
        News news = newsService.getById(newsId);
        if (news != null) {
            news.setNewsTitle(newsTitle);
            news.setNewsContent(newsContent);
            if(!imgs.isEmpty()){
                List<String> img = UpLoad.upload(imgs);
                if(!img.isEmpty()){
                    news.setCoverUrl(img.get(0));
                }
            }
            boolean res = newsService.updateById(news);
            if (res) {
                return Result.success("发布成功");
            }
        }
        return Result.error(RecruitExceptionEnum.UPDATE_FAILED);
    }
    @ApiOperation("资讯详情")
    @RequestMapping("/getNews")
    public Result newsInfo(int newsId) {
        News news = newsService.getById(newsId);
        if (news != null) {
                return Result.success(news);
        }
        return Result.error(RecruitExceptionEnum.NO_DATA);
    }
    @ApiOperation("添加资讯")
    @RequestMapping("/addNews/upload")
    public Result addNews(@RequestParam("newsTitle") String newsTitle,
                          @RequestParam("newsContent") String newsContent,
                          @RequestParam("img") List<MultipartFile> imgs,
                          HttpServletRequest request) {
        String token = request.getHeader("TOKEN");
        String username = JwtUtil.get(token, "username");
        News news = new News();
        news.setAdminName(username);
        news.setNewsTitle(newsTitle);
        news.setNewsContent(newsContent);
        if(!imgs.isEmpty()){
            List<String> img = UpLoad.upload(imgs);
            if(!img.isEmpty()){
                news.setCoverUrl(img.get(0));
            }
        }
        boolean save = newsService.save(news);
        if(save){
            return Result.success("发布成功");
        }
        return Result.error(RecruitExceptionEnum.ADD_FAILED);
    }
    @ApiOperation("上架下架资讯")
    @PutMapping("/shelveNews")
    public Result shelveNews(int newsId) {
        News news = newsService.getById(newsId);
        if (news != null) {
            Integer p = news.getState() == 1 ? 0 : 1;
            news.setState(p);
            boolean b = newsService.updateById(news);
            if (b) {
                return Result.success();
            }
        }
        return Result.error(RecruitExceptionEnum.HANDLE_FAILED);
    }
    @ApiOperation("删除资讯")
    @PutMapping("/delNews")
    public Result delNews(int newsId) {
        News news = newsService.getById(newsId);
        if (news != null) {
            news.setDeleteFlag(0);
            boolean b = newsService.updateById(news);
            if (b) {
                return Result.success();
            }
        }
        return Result.error(RecruitExceptionEnum.HANDLE_FAILED);
    }

    @RequestMapping("/newsCount")
    public Integer getNewsCount(){
        return newsService.count(null);
    }
}
